package siteflash.controller;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Calendar;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import siteflash.data.SiteFlashPersistenceManage;
import siteflash.model.Game;
import siteflash.model.ListGame;

public class CronGameFile extends HttpServlet {
public static final String BUCKETNAME = "static.y1s.org";
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws IOException {
		//Declare variable
		resp.setContentType("text/plain");
		String alias = (String) req.getParameter("alias");
		PersistenceManager psm = SiteFlashPersistenceManage.get().getPersistenceManager();
		String message = "";
		ListGame listGame = null;
		//Check game exits
		Key k = KeyFactory.createKey(ListGame.class.getSimpleName(), alias);
		
		try {
        	listGame = psm.getObjectById(ListGame.class, k);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		message = getGame(alias);
		resp.getWriter().println(message);
		System.out.println(alias);
			
			
	}
	
	public static String getGame(String alias) throws IOException {
		String content = "";
		String message = "";
		if (alias == null) {
			alias = "king_of_fighters_wing_1_7";
		}
		
		String url = "http://www.y8.com/games/" + alias;
		content = General.getUrl(url);
		
		Document doc = Jsoup.parse(content);
		Elements tr = doc.select("#main-table").select("tbody tr");
		
		
		//Set image
		Elements tableImage = doc.select("table.embed-table");
		String urlString = tableImage.select("tbody tr td").first().select("img").attr("src");
		String imageName = General.getName(urlString).replaceAll("[?0-9]*$", "");
		//Get image and save to google cloud Storage
		System.out.println(urlString + " - " + imageName);
		General.getFile(urlString, imageName);
		message += "Get image: " + urlString + " and rename to http://" + BUCKETNAME + "/" + imageName + "\n";
		
		//Set flash
		if (tr.get(1).select("object param").first() != null) {
			String flashString = "";
			if (tr.get(1).select("object param[name=movie]").first() != null) {
				flashString = tr.get(1).select("object param[name=movie]").first().val();
				//game.setType(new Text("flash"));
			} else {
				flashString = tr.get(1).select("object param[name=src]").first().val();
				//game.setType(new Text("shockwave"));
			}
			String flashName = General.getName(flashString).replaceAll("[?0-9]*$", "");
			int size = 0;
			//Get flash and save to google cloud storage
			size = General.getFile(flashString, flashName);
			message += "Get flash: " + flashString + " and rename to http://" + BUCKETNAME + "/" + flashName + "\n";
			//Set image
			String strSize = "";
			DecimalFormat dec = new DecimalFormat("0.00");
			if((size/1000000000)>0){
				strSize = dec.format((float)size/1000000000) + " GB";
			}

			else if((size/1000000)>0){
				
				strSize = dec.format((float)size/1000000) + " MB";
			}
			else if((size/1000)>0){
				strSize = dec.format((float)size/1000) + " kB";
			}
			else{
				strSize = size + " byte";
			}
			message += "size: " + strSize + "\n";
			System.out.println(strSize);
			
		} else if (tr.get(1).select("iframe").first() != null) {
			String flashString = tr.get(1).select("iframe").first().attr("src");
			String flashName = General.getName(flashString).replaceAll("[?cpcode=0-9]*$", "");
			System.out.println(flashString);
			int size = 0;
			//Get flash and save to google cloud storage
			size = General.getFile(flashString, flashName);
			message += "Get flash: " + flashString + " and rename to http://" + BUCKETNAME + "/" + flashName + "\n";
			//Set image
			String strSize = "";
			DecimalFormat dec = new DecimalFormat("0.00");
			if((size/1000000000)>0){
				strSize = dec.format((float)size/1000000000) + " GB";
			}

			else if((size/1000000)>0){
				
				strSize = dec.format((float)size/1000000) + " MB";
			}
			else if((size/1000)>0){
				strSize = dec.format((float)size/1000) + " kB";
			}
			else{
				strSize = size + " byte";
			}
			message += "size: " + strSize + "\n";
			System.out.println(strSize);
		} else {
			String flashString = tr.get(1).select("a[target=_blank]").select("img[class=external_game]").attr("src");
			String flashName = General.getName(flashString).replaceAll("[?0-9]*$", "");
			General.getFile(flashString, flashName);
			message += "Get flash: " + flashString + " and rename to http://" + BUCKETNAME + "/" + flashName + "\n";
			
			//Set new url
			String urlGameString = tr.get(1).select("a[target=_blank]").attr("href");
		}
		
			
		message += "Get image and flash game success: " + alias;
		System.out.println("Get image and flash game success: " + alias);
		
		return message;
	}
}
